package com.demo.freemarker.service;

import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoleDirectiveModel implements TemplateDirectiveModel {
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        System.out.println("==============");
        TemplateScalarModel user = (TemplateScalarModel) params.get("user");
        TemplateScalarModel role = (TemplateScalarModel) params.get("role");

        if("123456".equals(user.getAsString()) && "admin".equals(role.getAsString())) {
            loopVars[0] = TemplateBooleanModel.TRUE;
        }

        List<String> otherRights = new ArrayList<>();
        otherRights.add("add");
        otherRights.add("delete");
        otherRights.add("update");
        loopVars[1] = new SimpleSequence(otherRights);
        body.render(env.getOut());
    }
}
